package com.itheima.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.itheima.entity.SmsApiResult;


public class SMSUtils {


    public static SmsApiResult sendSmsCode(String mobile, String code){
        DefaultProfile profile = DefaultProfile.getProfile("default",
                "LTAI4Fbf7Xm94ewfZGXec1KW", "8uuTLLjkXUuK8FMyS4BvWttnhZ4CD0");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "传智健康");
        request.putQueryParameter("TemplateCode", "SMS_173479547");


        JSONObject json = new JSONObject();
        json.put("code",code);
        request.putQueryParameter("TemplateParam", json.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            SmsApiResult smsApiResult = JSON.parseObject(response.getData(), SmsApiResult.class);
            return smsApiResult;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

}
